//
//  MLZLogInView.m
//  MotherLover
//
//  Created by lanou on 16/1/8.
//  Copyright © 2016年 zj. All rights reserved.
//

#import "MLZLogInView.h"
#define MLR 10
#define MTB 10
@implementation MLZLogInView



-(instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        
        
        [self addViews:frame];
    }


    return self;


}




- (void)addViews:(CGRect)fram{

_imageView = [[UIButton alloc]initWithFrame:CGRectMake(((MLR+(KW-MLR)/2)/2),MTB, (KW-MLR)/2, (KW-MLR)/2)];
    [_imageView setBackgroundImage:[UIImage imageNamed:@"touxiang"]forState:(UIControlStateNormal)];
    
    [_imageView.layer setCornerRadius:(KW-MLR)/2/2];
    _imageView.layer.masksToBounds= YES;
    [self addSubview:_imageView];

// 邮箱
UILabel *mailL = [[UILabel alloc]initWithFrame:CGRectMake(_imageView.frame.origin.x, _imageView.bottom+MTB, 40, 30)];
mailL.text = @"账号:";

mailL.font = [UIFont systemFontOfSize:15];
[self addSubview:mailL];
_mailTF = [[UITextField alloc]initWithFrame:CGRectMake(mailL.right + 5, mailL.top, KW-mailL.right-MLR, mailL.height)];
_mailTF.backgroundColor = [UIColor whiteColor];
_mailTF.placeholder = @"请输入手机号/账号/邮箱";
    _mailTF.font = [UIFont systemFontOfSize:10];
_mailTF.inputAccessoryView = self.assView;
[self addSubview:_mailTF];

// 密码
UILabel *passwordL = [[UILabel alloc]initWithFrame:kNextFrame(mailL, 30)];
passwordL.text = @"密码:";
passwordL.font = [UIFont systemFontOfSize:15];
[self addSubview:passwordL];
_passwordTF = [[UITextField alloc]initWithFrame:CGRectMake(passwordL.right + 5, passwordL.top, 200, passwordL.height)];
_passwordTF.backgroundColor = [UIColor whiteColor];
_passwordTF.secureTextEntry = YES;
_passwordTF.inputAccessoryView = self.assView;
 _passwordTF.placeholder= @"请输入密码";
_passwordTF.font = [UIFont systemFontOfSize:10];
[self addSubview:_passwordTF];

// 下划线
PKLine *lab1 =[PKLine lineWithX:mailL.left Y:mailL.bottom Width:KW - 2*mailL.left];
[self addSubview:lab1];

PKLine *lab2 = [PKLine lineWithX:mailL.left Y:passwordL.bottom Width:KW - 2*mailL.left];
[self addSubview:lab2];

self.loginButt = [UIButton buttonWithType:(UIButtonTypeCustom)];
_loginButt.frame = CGRectMake(lab2.left, lab2.bottom + 30, lab2.width, 35);
_loginButt.backgroundColor = [UIColor greenColor];
[_loginButt setTitle:@"登陆" forState:(UIControlStateNormal)];
[self addSubview:_loginButt];

// 分割线
UILabel *friendsL = [[UILabel alloc]initWithFrame:CGRectMake(_loginButt.left+(_loginButt.width/2)/2, _loginButt.bottom+MTB, _loginButt.width/2, 20)];
friendsL.text = @"第三方登陆";
    friendsL.font = [UIFont systemFontOfSize:12];
  friendsL.adjustsFontSizeToFitWidth = YES;
    friendsL.textAlignment= NSTextAlignmentCenter;
[self addSubview:friendsL];
PKLine *line1 = [PKLine lineWithX:MLR Y:friendsL.top+MTB Width:friendsL.left-MLR*2];
[self addSubview:line1];
PKLine *line2 = [PKLine lineWithX:friendsL.right+MLR Y:friendsL.top+MTB Width:KW - friendsL.right-MLR*2 ];
[self addSubview:line2];



NSArray *arr = @[@"weibo", @"renren", @"douban", @"qq", ];
for (int i = 0; i < 4; i++){
    // 第三方按钮
    UIButton *butt = [UIButton buttonWithType:(UIButtonTypeCustom)];
    butt.layer.cornerRadius = (KW-MLR*4)/8/2;
    butt.layer.masksToBounds = YES;
    butt.tag = 4545 + i;
    butt.frame = CGRectMake(MLR + ((KW-MLR*4)/4+MLR)*i  , friendsL.bottom+30, (KW-MLR*4)/8, (KW-MLR*4)/8);
    butt.backgroundColor= [UIColor blackColor];
    [butt setImage:[UIImage imageNamed:arr[i]] forState:(UIControlStateNormal)];
    [self addSubview:butt];
}

}

// 辅助输入视图
- (UIView *)assView{
    if (!_assView){
        _assView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KW, 50)];
        _assView.backgroundColor = [UIColor whiteColor];
        
        // 左键
        UIButton *buttLeft = [UIButton buttonWithType:(UIButtonTypeCustom)];
        buttLeft.tag = 10;
        buttLeft.frame = CGRectMake(10, 10, 30, 30);
        [buttLeft setImage:[UIImage imageNamed:@"left1"] forState:(UIControlStateNormal)];
        [buttLeft addTarget:self action:@selector(click:) forControlEvents:(UIControlEventTouchUpInside)];
        [_assView addSubview:buttLeft];
        
        // 右键
        UIButton *buttRight = [UIButton buttonWithType:(UIButtonTypeCustom)];
        buttRight.tag = 11;
        buttRight.frame = CGRectMake(buttLeft.right + 10, 10, 30, 30);
        [buttRight setImage:[UIImage imageNamed:@"right1"] forState:(UIControlStateNormal)];
        [buttRight addTarget:self action:@selector(click:) forControlEvents:(UIControlEventTouchUpInside)];
        [_assView addSubview:buttRight];
        
        
        // 完成
        UIButton *okButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        okButton.frame = CGRectMake(KW - 60, 0, 50, 50);
        [okButton setTitle:@"完成" forState:(UIControlStateNormal)];
        [okButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [okButton addTarget:self action:@selector(okAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [_assView addSubview:okButton];
    }
    return _assView;
}

- (void)click:(UIButton *)button{
    BOOL isres = _mailTF.resignFirstResponder;
    NSLog(@"%d",isres);
    if(isres==1&&button.tag==11){
        
        [_passwordTF becomeFirstResponder];
        [_mailTF resignFirstResponder];
        
        
    }else if (isres==0&&button.tag==10){
        
        [_passwordTF resignFirstResponder];
        [_mailTF becomeFirstResponder];
        
    }else if (isres==1) {
        
        [_mailTF becomeFirstResponder];
        
    }
    
    
    
    
    
}

- (void)okAction:(UIButton *)butt{
    
    
    
    [_passwordTF resignFirstResponder];
    [_mailTF resignFirstResponder];
    
    
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_passwordTF resignFirstResponder];
    [_mailTF resignFirstResponder];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
